<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/17
 * Time: 上午11:28
 */
class Lyw_proofread_article_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function add($user_id, $title, $article_text, $article_html)
    {
        $data = array('proofread_user_id' => $user_id, 'title' => $title, 'article_text' => $article_text, 'article_html' => $article_html, 'creat_time' => time());
        $str = $this->db->insert_string('lyw_proofread_article', $data);
        $this->db->query($str);
        return $this->db->insert_id();
    }

    public function getOneListInfoById($id, $arr = array())
    {
        if (empty($arr)) {
            $sql = 'select * from lyw_proofread_article where id = ?';
        } else {
            $str = arrToStr($arr);
            $sql = "select {$str} from lyw_proofread_article where id = ?";
        }
        $result = $this->db->query($sql, array($id))->result_array();
        if (!empty($result)) {
            return $result[0];
        } else {
            return array();
        }
    }

    /**
     * 前面有个add方法,是在用户领取任务的时候产生的,这个方法是用户开始翻译的时候添加,其实就是update
     */
    public function saveAdd($proofread_id, $manager_id, $user_id, $title, $article_text, $article_html, $status, $url, $score)
    {
        $this->db->trans_start();
        $data = array('title' => $title, 'article_text' => $article_text, 'article_html' => $article_html, 'update_time' => time(), 'url' => $url, 'score' => $score);
        $where = "id = {$proofread_id} and proofread_user_id = {$user_id}";//必须带user_id ,有验证权限功能
        $str = $this->db->update_string('lyw_proofread_article', $data, $where);
        $this->db->query($str);
        if ($this->db->affected_rows() != 1) {
            return false;
        }
        $this->load->model('Lyw_article_manager_model');
        //修改管理表状态
        $this->Lyw_article_manager_model->changeStatus($manager_id, 'is_proofreaded', $status);
        $this->db->trans_complete();
        return true;
    }

    //任务专员字数统计
    public function getZYzishutongji()
    {
        $sql = "SELECT  a.id, a.proofread_user_id, CHAR_LENGTH(a.article_text) as article_length,b.username FROM lyw_proofread_article as a LEFT JOIN  lyw_user as b on a.proofread_user_id = b.id;";
        $result = $this->db->query($sql)->result_array();
        $arr = array();
        $return_arr = array();
        foreach($result as $k => $v){
            if(isset($arr[$v['proofread_user_id']])){
                $arr[$v['proofread_user_id']]['length'] += $v['article_length'];
            }else{
                $arr[$v['proofread_user_id']]['length'] = $v['article_length'];
                $arr[$v['proofread_user_id']]['username'] = $v['username'];
                $arr[$v['proofread_user_id']]['user_id'] = $v['proofread_user_id'];
            }
        }
        foreach($arr as $k => $v){
            array_push($return_arr , $v);
        }
        return $return_arr;
    }
}